#include<iostream>
#include<vector>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(nullptr) {}
};

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode dummyHead(0);
        dummyHead.next = head;
        ListNode *fast = &dummyHead;
        ListNode *slow = &dummyHead;

        while(n-- && fast->next != nullptr){
            fast = fast->next; 
        }
        fast = fast->next; // 再向前移动一位
        while(fast != nullptr){
            fast = fast->next;
            slow = slow->next;
        }
        slow->next = slow->next->next;
        return dummyHead.next;
    }
};

int main()
{
    Solution s;
    ListNode *head = new ListNode(1); // 创建一个新的链表节点，节点的值为1
    ListNode *current = head; // 创建指针指向链表头节点
    vector<int> values = {2, 3, 4, 5};
    for (int value : values) {
        current->next = new ListNode(value);
        current = current->next;
    }
    head = s.removeNthFromEnd(head, 2);
    
    // 打印删除节点后的链表
    current = head;
    while(current != nullptr){
        cout << current->val << " ";
        current = current->next;
    }
    return 0;
}